package org.ihelpuoo.util;

import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Stopwatch
{
	private static SimpleDateFormat	f	= new SimpleDateFormat("yy-MM-dd HH:mm:ss.S");

	private Calendar				b;
	private Calendar				e;

	public Calendar start()
	{
		long millis = System.currentTimeMillis();
		b = Calendar.getInstance();
		b.setTimeInMillis(millis);
		return b;
	}

	public Calendar stop()
	{
		long millis = System.currentTimeMillis();
		e = Calendar.getInstance();
		e.setTimeInMillis(millis);
		return e;
	}

	public long getTimeInMillis()
	{
		return e.getTimeInMillis() - b.getTimeInMillis();
	}

	@Override
	public String toString()
	{
		f.setDateFormatSymbols(new DateFormatSymbols());
		String re = getTimeInMillis() + "ms : ";
		re += f.format(b.getTime()) + " -> " + f.format(e.getTime());
		return re;
	}

}
